package io.lvdaxian.mybatis.session.defaults;

import io.lvdaxian.mybatis.mapping.MappedStatement;
import io.lvdaxian.mybatis.session.Configuration;
import io.lvdaxian.mybatis.session.SqlSession;

/**
 * DefaultSqlSession 是对象接口【SqlSession】的实现
 *
 * @author lihh
 */
public class DefaultSqlSession implements SqlSession {
  
  // 表示 控制上下文的配置文件
  private final Configuration configuration;
  
  public DefaultSqlSession(Configuration configuration) {
    this.configuration = configuration;
  }
  
  @Override
  public <T> T selectOne(String statement) {
    return (T) ("你被代理了！" + "方法：" + statement);
  }
  
  @Override
  public <T> T selectOne(String statement, Object parameter) {
    MappedStatement mappedStatement = configuration.getMappedStatement(statement);
    return (T) ("你被代理了！" + "\n方法：" + statement + "\n入参：" + parameter + "\n待执行SQL：" + mappedStatement.getSql());
  }
  
  @Override
  public <T> T getMapper(Class<T> clazz) {
    return configuration.getMapper(clazz, this);
  }
  
  @Override
  public Configuration getConfiguration() {
    return configuration;
  }
}
